$(function(){
	bindOrg2("orgSelect");
	bindDict("businessType","DM_XKXMLX","");
	
	$("#sxRadio1").click(function(){
		var val= $('#sxRadio1 input:radio:checked').val();
		if(val == "问题咨询"){
			$("#orgLabel").html("咨询机构");
		}else{
			$("#orgLabel").html("投诉机构");
		}
	});
	
	$.ajax({
		method: 'post',
	    url: basePath() + 'user/getUser.do',
	    async: false,
		contentType : 'application/json',
		data:JSON.stringify({}),
		dataType: 'json',
		success: function(data){
			if(data.result=="0"){
				$.alert({
				    title: '提示!',
				    content: '请先登录',
				    confirmButton: '确定'
				});
					
			}else{
				if($('#userName').length > 0 ){
					$('#userName').val(data.proposer);
				}
				
				if($('#mobile').length > 0 ){
					$('#mobile').val(data.mobilephone);
				}
				
				if($('#email').length > 0 ){
					$('#email').val(data.email);
				}
			}
		}
	});
});


function save(){
	var consultingData = $("#consultingForm").serializeObject();
	consultingData.orgCode = $('#orgSelect').attr("data");
	if(!check(consultingData))
		return true;
	
	$.ajax({ 
		method: 'post',
		url: basePath() + 'consulting/addConsulting.do',
		data: JSON.stringify(consultingData),
		dataType: "json",
        contentType: "application/json",
		success: function(data){	
			$.alert(data.info);				

	    	if(data.result == 1){
	    		parent.refreshData();//刷新数据
	    	} 
      	}
		
	});
	
}

function check(data){	
	var result = true;

	var mustStr = "complainType,userName,mobile,email,complainTitle,leaveMessage,orgName";
	
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for(var i=0; i<mustStrs.length; i++){
		var obj = $('[name="'+mustStrs[i]+'"]');
		if(obj.val() == ""){
			strMsg += obj.attr("placeholder") + "！<br />";
			
			result = false;
		}
	}
	
	//手机号验证
	var phoneNum = $('#mobile').val();
	phoneNum = phoneNum.replace(/[ ]/g,"");//去空格
	var phoneReg = /^1\d{10}$/;
	if(!phoneReg.test(phoneNum)){
		strMsg += "手机号不合法!<br />"
		result = false;
	}
	
	//邮箱验证
	var emailNum = $('#email').val();
	emailNum = emailNum.replace(/[ ]/g,"");//去空格
	var emailReg = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
	if(!emailReg.test(emailNum)){
		strMsg += "邮箱不合法!<br />"
		result = false;
	}
	

	if(strMsg != ""){
		$.alert(strMsg);
	}
	
	return result;
}

function bindOrg1(selectId){

	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			data.hot = [];
		    var cityPicker = new IIInsomniaCityPicker({
		        data: data,
		        target: '#' + selectId,
		        callback: function(id){
		        	var id = $('#' + selectId).attr("data");
		        	var name = $('#' + selectId).val();
		        }
		    });

		    cityPicker.init();
		}
	});
}

function bindOrg2(selectId){
	$.ajax({ 
		method: 'post',
		url: projectPath + "share/getOrg.do",
		dataType: "json",
		contentType: "application/json",
		success: function(data){
			//历史海事机构
			var historyjson = data.hisResult;
			//海事机构
			var orgAlljson = data.result;
			
			var topCode = '000000';
			var orgList = JSON2ARRAY3(orgAlljson, topCode);
			var orgHistoryList = JSON2ARRAY2(historyjson);
			InitList(selectId, topCode, orgList, orgHistoryList);
		}
	});
}
